<?php
session_start(); //开启session会话 类似于sessionStorage
header('Content-type:text/html;charset=utf-8');//设置编码格式
include_once 'dbconfig.php';
if (isset($_POST['name']) && isset($_POST['type']) && $_POST['type'] == 'getName') {
    $name = $_POST['name'];

    $selectStr = 'select * from user where userName="' . $name . '"';
    $result = $link->query($selectStr);
    if ($result->num_rows > 0) {
        $msg = ['msg' => '用户名已注册', 'status' => 1];
        responseEvent($msg);
    } else {
        $msg = ['msg' => '用户名未注册', 'status' => 0];
        responseEvent($msg);
    }
    echo json_encode($msg);
} else if (isset($_POST['name']) && isset($_POST['psw']) && isset($_POST['psw'])) {
    $name = $_POST['name'];
    $psw = $_POST['psw'];
    $code=$_POST['code'];
    $selectStr = 'select password from user where userName="' . $name . '"';
    $result = $link->query($selectStr);
    if ($result->num_rows > 0) {
        $datas = [];
        while ($data = $result->fetch_assoc()) {
            array_push($datas, $data);
        }
        if ($datas[0]['password'] != md5($psw)) {
            $msg = ['msg' => '*密码错误!', 'status' => 2];
            responseEvent($msg);
        }
    }
    if ($code != strtolower($_SESSION['validate'])) {
        $msg = ['msg' => '*验证码有误！', 'status' => 3];
        responseEvent($msg);
    }

    $msg = ['msg' => '登录成功', 'status' => 1];
    responseEvent($msg);
}

function responseEvent($msg)
{
    global $link;
    echo json_encode($msg);
    $link->close();
    exit();
}